#==============================================================================
# ** Window_BattleStatus
#------------------------------------------------------------------------------
#  This window displays the status of all party members on the battle screen.
#==============================================================================

class Window_BattleStatus < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  attr_reader   :_bar_energy
  attr_reader   :index
  attr_reader   :timer
  attr_reader   :_player_state_bars
  attr_reader   :_enemy_state_bars
  def initialize
    super(0, 320, 640, 160)
    self.opacity = 100
    self.contents = Bitmap.new(width - 32, height - 32)
    @level_up_flags = [false, false, false, false]
    @_bar_energy = Sprite_Bar.new
    @_bar_energy.set_energy_bar
    @timer = 1
    @index = 1
    @_player_state_bars = []
    @_enemy_state_bars = []
    refresh
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    super
    @_bar_energy.dispose
    for item in @_player_state_bars
      item.dispose
    end
    for item in @_enemy_state_bars
      item.dispose
    end
  end
  #--------------------------------------------------------------------------
  # * Set Level Up Flag
  #     actor_index : actor index
  #--------------------------------------------------------------------------
  def level_up(actor_index)
    @level_up_flags[actor_index] = true
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    @_bar_energy.refresh($game_party.battle_energy, 10)
    for item in @_player_state_bars
      item.refresh
    end
    for i in 0...@_enemy_state_bars.size do
      item = $game_troop.enemies[i]
      bar = @_enemy_state_bars[i]
      if item.dead?
        bar.visible = false if bar.visible
      else
        bar.refresh(item.hp, item.maxhp)
      end
    end
    if @timer > 0
      @timer -= 1
      return
    end
    if @_player_state_bars.size == 0
      for i in 0...$game_party.actors.size
        actor = $game_party.actors[i]
        item = Window_PlayerBattler.new(actor, i)
        @_player_state_bars.push(item)
      end
      for item in $game_troop.enemies
        bar = Sprite_Bar.new
        bar.set_enmey_state_bar(item)
        @_enemy_state_bars.push(bar)
      end
    end
    @timer = 30
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    # Slightly lower opacity level during main phase
    if $game_temp.battle_main_phase
      self.contents_opacity -= 4 if self.contents_opacity > 191
    else
      self.contents_opacity += 4 if self.contents_opacity < 255
    end
  end
end
